//
//  DIsplayCarRootViewController.m
//  CarHome
//
//  Created by lanou3g on 15-4-27.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "DIsplayCarRootViewController.h"
#import "DisplayCarNavigationBar.h"
#import "SummaryTableViewController.h"
#import "PictureTableViewController.h"
#import "DealerTableViewController.h"
#import "AlibiTableViewController.h"
#import "NewsTableViewController.h"
#import "BBSTableViewController.h"

@interface DIsplayCarRootViewController ()  <UIScrollViewDelegate>

@property (nonatomic, retain) UIView *rootView;
@property (nonatomic, retain) DisplayCarNavigationBar *navigationBar;
@property (nonatomic, retain) UIScrollView *scrollView;
@property (nonatomic, retain) SummaryTableViewController *summaryTVC;
@property (nonatomic, retain) PictureTableViewController *pictureTVC;
@property (nonatomic, retain) DealerTableViewController *dealerTVC;
@property (nonatomic, retain) AlibiTableViewController *alibiTVC;
@property (nonatomic, retain) NewsTableViewController *newsTVC;
@property (nonatomic, retain) BBSTableViewController *bbsTVC;
@property (nonatomic, retain) UIWebView *explainView;

@property (nonatomic, assign) CGFloat width;
@property (nonatomic, assign) CGFloat height;

@end

@implementation DIsplayCarRootViewController

- (void)loadView {
    
    [self addRootView];
    
    [self addNavigationBar];
    
    [self addScrollView];
}

- (void)addRootView {
    self.rootView = [[[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds] autorelease];
    self.view = _rootView;
}

- (void)addNavigationBar {
    [self.navigationController setNavigationBarHidden:YES];
    self.navigationBar = [[DisplayCarNavigationBar alloc] initWithFrame:CGRectMake(0, 20, kWIDTH, kBTN_HEIGHT * 2)];
    [_rootView addSubview:_navigationBar];
}

- (void)addScrollView {
    self.scrollView = [[[UIScrollView alloc] initWithFrame:CGRectMake(0, kBTN_HEIGHT * 2 + 20, _rootView.frame.size.width, _rootView.frame.size.height - kBTN_HEIGHT)] autorelease];
    _scrollView.backgroundColor = [UIColor whiteColor];
    _width = _scrollView.frame.size.width;
    _height = _scrollView.frame.size.height;
    [_rootView addSubview:_scrollView];
    
    self.summaryTVC = [SummaryTableViewController new] ;
    _summaryTVC.seriesID = _seriesID;
    _summaryTVC.tableView.frame = CGRectMake(0, 0, _width, _height - 80);
    [_scrollView addSubview:_summaryTVC.tableView];
    [self addChildViewController:_summaryTVC];
    
    self.pictureTVC = [PictureTableViewController new];
    _pictureTVC.seriesID = _seriesID;
    _pictureTVC.tableView.frame = CGRectMake(kWIDTH, 0, _width, _height - 80);
    
    [_scrollView addSubview:_pictureTVC.tableView];
    [self addChildViewController:_pictureTVC];
    
    self.dealerTVC = [DealerTableViewController new];
    _dealerTVC.seriesID = _seriesID;
    _dealerTVC.tableView.frame = CGRectMake(kWIDTH * 2, 0, _width, _height - 80);
    [_scrollView addSubview:_dealerTVC.tableView];
    [self addChildViewController:_dealerTVC];
    
    self.alibiTVC = [AlibiTableViewController new];
    _alibiTVC.seriesID = _seriesID;
    _alibiTVC.tableView.frame = CGRectMake(kWIDTH * 3, 0, _width, _height - 80);
    [_scrollView addSubview:_alibiTVC.tableView];
    [self addChildViewController:_alibiTVC];
    
    
    self.newsTVC = [NewsTableViewController new];
    _newsTVC.seriesID = _seriesID;
    _newsTVC.tableView.frame = CGRectMake(kWIDTH * 4, 0, _width, _height - 80);
    [_scrollView addSubview:_newsTVC.tableView];
    [self addChildViewController:_newsTVC];
    
    self.bbsTVC = [BBSTableViewController new];
    _bbsTVC.seriesID = _seriesID;
    _bbsTVC.tableView.frame = CGRectMake(kWIDTH * 5, 0, _width, _height - 80);
    [_scrollView addSubview:_bbsTVC.tableView];
    [self addChildViewController:_bbsTVC];

    self.explainView = [[UIWebView alloc] initWithFrame:CGRectMake(kWIDTH * 6, 0, _width, _height - 80)];
    [_scrollView addSubview:_explainView];
    
    _scrollView.contentSize = CGSizeMake(_width * 7, 0);
    _scrollView.pagingEnabled = YES;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self navigationBarAddTarget];

}

- (void)navigationBarAddTarget {
    
    [_navigationBar.returnBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_navigationBar.summaryBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_navigationBar.pictureBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_navigationBar.dealerBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_navigationBar.alibiBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_navigationBar.newsBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_navigationBar.explainBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_navigationBar.bbsBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    _scrollView.delegate = self;
}

- (void)btnAction:(UIButton *)sender {
    
    NSInteger btnTagNum = sender.tag;
    if (btnTagNum == 100) {
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        [sender setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [self animationOfNBLabelWithBtnTagNum:btnTagNum];
        [self grayOhterButtonTintColorWithTag:btnTagNum];
        [self scrollviewSetContentOffsetWithBtnTagNum:btnTagNum];
        if (btnTagNum != 107) {
            [self.childViewControllers[btnTagNum - 101] loadData];
        } else {
            [_explainView addMyActivityIndicatorView2];
            [_explainView loadRequest:[self getURLRequest]];
            [_explainView removeMyActivityIndicatorViewFromSuperView];
            [_explainView reload];
        }
    }
    
}

- (void)animationOfNBLabelWithBtnTagNum:(NSInteger)btnTagNum {
    CGRect rect = _navigationBar.labelRow1.frame;
    [UIView setAnimationDuration:.3];
    if (btnTagNum > 100 && btnTagNum < 104 && rect.origin.y == kBTN_HEIGHT - 1) {
        [UIView beginAnimations:nil context:nil];
        rect.origin.x = kWIDTH / 4 * (btnTagNum - 100);
        _navigationBar.labelRow1.frame = rect;
        [UIView commitAnimations];
    } else if (btnTagNum > 103 && btnTagNum < 108 && rect.origin.y == kBTN_HEIGHT - 1) {
        rect.origin.x = kWIDTH / 4 * (btnTagNum - 104);
        rect.origin.y = kBTN_HEIGHT * 2 - 2;
        _navigationBar.labelRow1.frame = rect;
    } else if (btnTagNum > 100 && btnTagNum < 104 && rect.origin.y == kBTN_HEIGHT * 2 - 2) {
        rect.origin.x = kWIDTH / 4 * (btnTagNum - 100);
        rect.origin.y = kBTN_HEIGHT - 1;
        _navigationBar.labelRow1.frame = rect;
    } else if (btnTagNum > 103 && btnTagNum < 108 && rect.origin.y == kBTN_HEIGHT * 2 - 2) {
        [UIView beginAnimations:nil context:nil];
        rect.origin.x = kWIDTH / 4 * (btnTagNum - 104);
        _navigationBar.labelRow1.frame = rect;
        [UIView commitAnimations];
    }
    
}

- (void)grayOhterButtonTintColorWithTag:(NSInteger)tag {
    for (int i = 101; i < 108; i++) {
        if (i == tag) {
            continue;
        }
        UIButton *temp = (UIButton *)[_navigationBar viewWithTag:i];
        [temp setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    }
}

- (void)scrollviewSetContentOffsetWithBtnTagNum:(NSInteger)btnTagNum {
    CGPoint offset = CGPointMake(kWIDTH * (btnTagNum - 101), 0);
    [_scrollView setContentOffset:offset animated:YES];
}

//结束回弹的时候！！！！
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    CGFloat offsetX = scrollView.contentOffset.x;
    NSInteger btnTagNum = 101 + offsetX / kWIDTH;
    UIButton *button = (UIButton *)[_navigationBar viewWithTag:btnTagNum];
    [self btnAction:button];
//    [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
//    [self grayOhterButtonTintColorWithTag:btnTagNum];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    CGFloat offsetX = scrollView.contentOffset.x;
    NSInteger btnTagNum = 101 + offsetX / kWIDTH;
    UIButton *button = (UIButton *)[_navigationBar viewWithTag:btnTagNum];
    [self btnAction:button];
}

- (NSURLRequest *)getURLRequest {
    
    /**
     给NSString写延展方法：
     @interface NSString (stringByDecodingURLFormat)
     - (NSString *)stringByDecodingURLFormat;
     @end
     
     @implementation NSString
     - (NSString *)stringByDecodingURLFormat
     {
     NSString *result = [(NSString *)self stringByReplacingOccurrencesOfString:@"+" withString:@" "];
     result = [result stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
     return result;
     }
     @end
     
     使用实例：
     NSString *decodedString = [myString stringByDecodingURLFormat];
     
     */
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
    NSString *name_gb2312 = [_seriesName stringByAddingPercentEscapesUsingEncoding:enc];
//    NSLog(@"---%@", name_gb2312);
//    NSLog(@"====%@", ExplainURLwithCarID(_seriesID, name_gb2312));
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString: ExplainURLwithCarID(_seriesID, name_gb2312)]];
    return request;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
